/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/***********************************************************************
*				Avionics Project
*
* FILENAME:
* aiTest.c
*
* DESCRIPTION
* This is to test the Analog input driver for the DM6210 AI.
*
* modification history
* --------------------
* $Date: 2005/05/03 04:39:31 $
* $Revision: 1.1.1.1 $
* $Author: dixonc $
*
***********************************************************************/

#include <vxWorks.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <ioLib.h>
#include <taskLib.h>
#include <sysLib.h>

#include "fileLog.h"
#include "String.h"
#include "timeLib.h"
#include "queue.h"
#include "type_defs.h"

#define MAX_AI_READ        1024
#define NUM_AIDIO_AI       16

// functions
void aiUsage();		  // prints usage description

volatile static BOOL aiRun;

//----------------------------------------------------------
// aiStop
//	stop the execution of an AI test
//----------------------------------------------------------
void aiStop()
{
	aiRun = FALSE;
}

//----------------------------------------------------------
// aiDisplay
//	display the voltages present on the AI pins
//
// Paramters 
//	pins - 	binary value of which of the 16 pins to use
//----------------------------------------------------------
void aiDisplay(unsigned short pins)
{
	int aiFd[NUM_AIDIO_AI];
	float voltage = 0;
	int i;

	aiRun = TRUE;

	for( i=0; i < NUM_AIDIO_AI; i++) {
		if( (pins >> i) & 0x01 ){
			char name[20];
			(void)sprintf(name, "/ai/%i",i);

			if( (aiFd[i] = open(name, 0, 0)) == ERROR )
				perror("aiOpen");
		}
	}

	while( aiRun )
	{
		printf("[1;1H");
		for( i=0; i < NUM_AIDIO_AI; i++)
		{
			if( (pins >> i) & 0x01 ){
				if( read(aiFd[i], (char *)&voltage, 1) != 1 )
					perror("aiRead");

				if( i == 8 )
					printf("\n");

				printf("%2.3f     ", voltage);
			}
		}
		printf("\n");

		/* don't kill the machine */
		taskDelay( sysClkRateGet() / 2 );
	}

	for( i=0; i < NUM_AIDIO_AI; i++)
		if( (pins >> i) & 0x01 )
			if( close(aiFd[i]) == ERROR )
				perror("aiClose");

}
//----------------------------------------------------------
// aiTime
//	Time the AI read function
//
// Paramters 
//----------------------------------------------------------

void aiTime()
{
	int aiFd[1];
	float voltage[MAX_AI_READ];
	TimeStamp start;
	TimeStamp stop;
	float msec;
	Queue timeQueue(MAX_AI_READ);

	if( (aiFd[0] = open("/ai/0", O_RDONLY, 0)) == ERROR ) {
		perror("aiOpen");
		return;
	}

	for( int i=1; i <= MAX_AI_READ; i *= 2)
	{
		// start time 
		start.stamp();
	
		if( read(aiFd[0], (char *)&voltage, i) != i )
			perror("aiRead");

		// start time 
		stop.stamp();

		// get time difference and put on queue
		msec = (stop - start) / 1000.0f;
		timeQueue.Push( (QueueType *)&(msec) ); // save as milli-sec
	}

	// save timeQueue to file
	timeQueue.saveToFile("/tffs0/ai_poll_time.dat" ); // save as milli-sec

	if( close(aiFd[0]) == ERROR )
		perror("aiClose");

}


